﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Espam.Idiomas.Decimo.Tesis.Negocio;
using Microsoft.Reporting.WebForms;


namespace Espam.Idiomas.Decimo.Tesis.Web
{
    public partial class SiteMaster : MasterPage
    {
        private const string AntiXsrfTokenKey = "__AntiXsrfToken";
        private const string AntiXsrfUserNameKey = "__AntiXsrfUserName";
        private string _antiXsrfTokenValue;

        protected void Page_Init(object sender, EventArgs e)
        {
            // The code below helps to protect against XSRF attacks
            var requestCookie = Request.Cookies[AntiXsrfTokenKey];
            Guid requestCookieGuidValue;
            if (requestCookie != null && Guid.TryParse(requestCookie.Value, out requestCookieGuidValue))
            {
                // Use the Anti-XSRF token from the cookie
                _antiXsrfTokenValue = requestCookie.Value;
                Page.ViewStateUserKey = _antiXsrfTokenValue;
            }
            else
            {
                // Generate a new Anti-XSRF token and save to the cookie
                _antiXsrfTokenValue = Guid.NewGuid().ToString("N");
                Page.ViewStateUserKey = _antiXsrfTokenValue;

                var responseCookie = new HttpCookie(AntiXsrfTokenKey)
                {
                    HttpOnly = true,
                    Value = _antiXsrfTokenValue
                };
                if (FormsAuthentication.RequireSSL && Request.IsSecureConnection)
                {
                    responseCookie.Secure = true;
                }
                Response.Cookies.Set(responseCookie);
            }

            Page.PreLoad += master_Page_PreLoad;
        }

        protected void master_Page_PreLoad(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Set Anti-XSRF token
                ViewState[AntiXsrfTokenKey] = Page.ViewStateUserKey;
                ViewState[AntiXsrfUserNameKey] = Context.User.Identity.Name ?? String.Empty;
            }
            else
            {
                // Validate the Anti-XSRF token
                if ((string)ViewState[AntiXsrfTokenKey] != _antiXsrfTokenValue
                    || (string)ViewState[AntiXsrfUserNameKey] != (Context.User.Identity.Name ?? String.Empty))
                {
                    throw new InvalidOperationException("Validation of Anti-XSRF token failed.");
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string validaSession;
           
                Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
                Response.Cache.SetAllowResponseInBrowserHistory(false);
                Response.Cache.SetNoStore();

                if (Session["userEspamIdiomasVarSession001"] == null || Session["rolEspamIdiomasVarSession003"] == null)
                {
                    validaSession = "NoHaIniciadoSession";
                    Response.Redirect("UI_Login.aspx?validaSession=" + validaSession);
                }
                else if (Session["userEspamIdiomasVarSession001"] != null && Session["rolEspamIdiomasVarSession003"].ToString() == "Docente")
                {
                    Response.Redirect("UI_DashboardTeacher.aspx");
                }
                else if (Session["userEspamIdiomasVarSession001"] != null && Session["rolEspamIdiomasVarSession003"].ToString() == "Administradores")
                {
                    try
                    {
                        Validaciones oValidaciones = new Validaciones();
                        Usuarios ousu = new Usuarios();
                        string nombre = "";
                        string apellido = "";
                        string[] name;
                        string[] lastName; 
                        imgProfile.ImageUrl = "UploadedImagesTeachers/" + Session["cadPicEspamIdiomasVarSession005"].ToString()+".jpg";
                        imgProfile1.ImageUrl = "UploadedImagesTeachers/" + Session["cadPicEspamIdiomasVarSession005"].ToString()+".jpg";
                        nombre = Session["nameEspamIdiomasVarSession006"].ToString();
                        apellido = Session["lastNameEspamIdiomasVarSession005"].ToString();
                        lblApellido.Text = apellido;
                        name=oValidaciones.UserName(nombre);
                        lblName.Text = name[0];
                        lastName = oValidaciones.UserName(apellido);
                        lblName1.Text = name[0];
                        lblApellido1.Text = lastName[0];
                        //EnLinea();
                        UsersInLine();
                    }
                    catch (Exception)
                    {
                        
                    }
                }
                else if (Session["userEspamIdiomasVarSession001"] != null && Session["rolEspamIdiomasVarSession003"].ToString() == "Alumno")
                {
                    Response.Redirect("UI_DashboardStudents.aspx");
                }
       }
        //private void EnLinea()
        //{
        //    lblEnLinea.Text = OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.RegistredUsersCount.ToString();
        //}
        private void UsersInLine()
        {
             int currentNumberOfUsers = Global.CurrentNumberOfUsers;
             int totalNumberOfUsers = Global.TotalNumberOfUsers;
             lblEnLinea.Text = currentNumberOfUsers.ToString();

        }

        protected void Timer2_Tick(object sender, EventArgs e)
        {

        }


            
    }
}